package com.miraclemiles.oauth.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Service;

/**
 * token管理
 *
 * @auther zhoudong
 * @date 2021/9/3 13:11
 */

@Service
public class OauthTokenService {

    @Autowired
    private TokenStore tokenStore;

    /**
     * 移除token
     * @param token
     */
    public void removeToken(String token) {
        OAuth2AccessToken existingAccessToken = tokenStore.readAccessToken(token);
        //token无效处理
        if (null == existingAccessToken) {
            //response.sendError(HttpStatus.BAD_REQUEST.value(), "Token was not recognised");
        } else {
            //token有效,登出处理
            if (existingAccessToken.getRefreshToken() != null) {
                tokenStore.removeRefreshToken(existingAccessToken.getRefreshToken());
            }
            tokenStore.removeAccessToken(existingAccessToken);
        }
    }
}
